<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Position;
use App\Models\FirmProfession;
use App\Models\Post;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class PositionController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Position(['post:id,post_name','profession:id,name']), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('name');
            $grid->column('post.post_name','岗位名称');
            $grid->column('profession.name','行业');
            $grid->column('annual_salary');
            $grid->column('company_name');
            $grid->column('address');
            $grid->column('status')
                 ->using([1 => '发布',2 => '取消发布'])
                 ->label([1 => 'success',2 => 'danger']);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->like('name')
                       ->width(3);

                $filter->equal('post_id')
                    ->select(Post::getOption())
                    ->width(3);
                $filter->equal('firm_profession_id')
                    ->select(FirmProfession::getOption())
                    ->width(3);

                $filter->like('annual_salary')
                    ->width(3);


                $filter->like('company_name')
                    ->width(3);


            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Position(), function (Show $show) {
            $show->field('id');
            $show->field('name');
            $show->field('annual_salary');
            $show->field('company_name');
            $show->field('address');
            $show->field('duty');
            $show->field('demand');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Position(), function (Form $form) {
            $form->display('id');
            $form->text('name')
                 ->maxLength(255)
                 ->required();
            $form->select('firm_profession_id','企业行业')
                 ->options(FirmProfession::getOption())
                 ->required();
            $form->select('post_id','职位')
                 ->options(Post::getOption())
                 ->required();
            $form->text('annual_salary')
                ->maxLength(255)
                ->required();
            $form->text('company_name')
                ->maxLength(255)
                ->required();

            $form->distpicker(['province','city','area'],'区域')
                 ->required();
            $form->text('address')
                ->maxLength(255)
                ->required();
            $form->editor('duty')
                 ->required();
            $form->editor('demand')
                 ->required();
            $form->number('view_count')
                 ->min(0)
                 ->default(0)
                 ->required();
            $form->radio('status')
                 ->options([1 => '发布',2 => '取消发布'])
                 ->default(1)
                 ->required();

            $form->display('created_at');
            $form->display('updated_at');
        });
    }
}
